<template>
  <!-- 工序表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="375px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="工序编号" prop="processCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.processCode"
              placeholder="请输入工序编号"
              clearable
            />

            <span v-else>{{ formData.processCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="工序名称" prop="processName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.processName"
              placeholder="请输入工序名称"
              clearable
            />

            <span v-else>{{ formData.processName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="是否启用" prop="enabled">
            <el-radio-group v-model="formData.enabled" v-if="!props.isReadonly">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>

            <span v-else>{{ formData.enabled ? "启用" : "停用" }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.remarks"
              type="textarea"
              placeholder="请输入备注"
              clearable
            />

            <span v-else class="text-wrap">{{ formData.remarks }}</span>
          </el-form-item>
        </el-col>

        <el-form-item
          label="创建时间"
          prop="createTime"
          v-if="props.isReadonly"
        >
          <span>{{ formData.createTime }}</span>
        </el-form-item>
        <el-form-item
          label="创建人"
          prop="createdUserName"
          v-if="props.isReadonly"
        >
          <span>{{ formData.createdUserName }}</span>
        </el-form-item>
        <el-form-item
          label="修改时间"
          prop="lastUpdateTime"
          v-if="props.isReadonly"
        >
          <span>{{ formData.lastUpdateTime }}</span>
        </el-form-item>
        <el-form-item
          label="修改人"
          prop="lastUpdatedUserName"
          v-if="props.isReadonly"
        >
          <span>{{ formData.lastUpdatedUserName }}</span>
        </el-form-item>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MDMMDMPROCESSADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  version: [],
  processCode: [
    { required: true, message: "请输入工序编号", trigger: "blur" },
    { min: 1, max: 20, message: "长度在 1 到 20 个字符", trigger: "blur" },
  ],
  processName: [
    { required: true, message: "请输入工序名称", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  processCode: "",
  processName: "",
  enabled: 1,
  deleted: "",
  remarks: "",
  createTime: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        processCode,
        processName,
        enabled,
        remarks,
        createTime,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        processCode,
        processName,
        enabled,
        remarks,
        createTime,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.processCode = "";
  formData.processName = "";
  formData.enabled = 1;
  formData.remarks = "";
}

const loading = ref(false);

/** 工序保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mdm/mdmprocess/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>

<style scoped>
.text-wrap {
  white-space: pre-wrap; /* 保留空白符序列，但是正常地进行换行 */
  word-break: break-all; /* 在任意字符处换行 */
}
</style>
